home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / IFC_112 / netscape / application / HTMLParsingRules.java < prev    next >
Encoding:
Text File  |  1999-05-28  |  11.8 KB  |  305 lines  |  [TEXT/CWIE]

  1. // HTMLParsingRules.java
  2. // By Ned Etcode
  3. // Copyright 1995, 1996, 1997 Netscape Communications Corp. All rights reserved.
  4.  
  5. package netscape.application;
  6.  
  7. import netscape.util.*;
  8.  
  9. /** Instance of this class describe the rules used to parse some html.
  10.   *
  11.   */
  12. public class HTMLParsingRules implements Codable {
  13.     private Hashtable rules=null;
  14.     private String    defaultContainerClassName;
  15.     private String    defaultMarkerClassName;
  16.  
  17.     static final String RULES_KEY = "rules";
  18.     static final String DEFAULT_CONTAINER_CLASS_NAME_KEY = "defaultContainerClass";
  19.     static final String DEFAULT_MARKER_CLASS_NAME_KEY    = "defaultMarkerClass";
  20.  
  21.     /*
  22.      * HTML Default rules
  23.      */
  24.     private  static final String HTMLDefaultRules =
  25.         "{" +
  26.         "  LI = { BeginTermination = ( LI ); EndTermination = (OL,UL,DIR,MENU); };   " +
  27.         "  IMG= { IsContainer=false; };                                     " +
  28.         "  DD = { BeginTermination = (DT,DD); EndTermination = (DL,A); };     " +
  29.         "  DT = { BeginTermination = (DT,DD); EndTermination = (DL,A); };     " +
  30.         "   P = { IsContainer=true; BeginTermination=(P,OL,UL,DIR,MENU,PRE,H1,H2,H3,H4,H5,H6);  }; " +
  31.         "  BR = { IsContainer=false; };                                     " +
  32.         "  HR = { IsContainer=false; };                                     " +
  33.         "  PRE= { ShouldRetainFormatting=true; };                              " +
  34.         "    A= { IsContainer=true; BeginTermination = (A); };              " +
  35.         "}";
  36.  
  37.     /** This is not a real HTML marker, however this marker is used
  38.       * to mean "a String"
  39.       */
  40.     public static final String STRING_MARKER_KEY  = "IFCSTRING";
  41.  
  42.     /** This is not a real HTML marker, however this marker is used
  43.       * to mean "a Comment"
  44.      */
  45.     public static final String COMMENT_MARKER_KEY = "IFCCOMMENT";
  46.  
  47.     /**
  48.       * The following keys are used to define how to parse HTML markers.
  49.       * the default value of the "rule database" is:'
  50.       *
  51.       *  {
  52.       *    LI = { BeginTermination = ( LI ); EndTermination = (OL,UL); };
  53.       *    IMG= { IsContainer=false; };
  54.       *    DD = { BeginTermination = (DT,DD); EndTermination = (DL); };
  55.       *    DT = { BeginTermination = (DT,DD); EndTermination = (DL); };
  56.       *     P = { BeginTermination = (P,H1,H2,H3,H4,H6,TABLE);
  57.       *          EndTermination = (BODY,HTML);};
  58.       *    BR = { IsContainer=false; };
  59.       *    HR = { IsContainer=false; };
  60.       *    PRE= { ShouldRetainFormatting=true; };
  61.       *      A= { IsContainer=true; BeginTermination = (A); };
  62.       *  };
  63.       */
  64.  
  65.  
  66.     /** The class that should be used to represent an HTML component
  67.       * There is no default value although it is possible to set a default class
  68.       * for a String, a Container or a marker
  69.       */
  70.     public static final String REPRESENTATION_KEY              = "Representation";
  71.  
  72.     /** This parameter is a list of markers. If the parser finds this marker in
  73.       * a begin form (<FOO>)while parsing the HTML component, it will assume the
  74.       * end of the component.
  75.       */
  76.     public static final String BEGIN_TERMINATION_MARKERS_KEY   = "BeginTermination";
  77.  
  78.     /** This parameter is a list of markers. If the parser finds this marker in
  79.       * an ending form (</FOO>) while parsing the HTML component, it will assume
  80.       * the end of the component
  81.       */
  82.     public static final String END_TERMINATION_MARKERS_KEY     = "EndTermination";
  83.  
  84.  
  85.     /** This parameter defines whether the html component is a container or not.
  86.       * default value is true
  87.       */
  88.     public static final String IS_CONTAINER_KEY                     = "IsContainer";
  89.  
  90.     /** This parameter defines whether the html component requires the strings
  91.       * inside itself to be formated or not.
  92.       * default value is false
  93.       */
  94.     public static final String SHOULD_RETAIN_FORMATTING_KEY       = "ShouldRetainFormatting";
  95.  
  96.     /** If true, the end of marker should be ignored. This is currently used
  97.       * for </P>. Default value is false
  98.       */
  99.     public static final String SHOULD_IGNORE_END_KEY           = "ShouldIgnoreEnd";
  100.  
  101.     public HTMLParsingRules() {
  102.         rules = (Hashtable) Deserializer.deserializeObject( HTMLDefaultRules );
  103.         if( rules == null )
  104.             throw new InconsistencyException("HTMLParsingRules: Cannot deserialize default rules");
  105.     }
  106.  
  107.     /** Set the rules for a given marker. You can use this API to teach the
  108.       * parser how unsupported markers behave.
  109.       * Possible keys are:
  110.       *    REPRESENTATION_KEY:       (Class)  the class that should be used to represent the marker
  111.       *    BEGIN_TERMINATION_MARKERS_KEY:  (Vector) list of marker that terminate the marker
  112.       *                                             when they are beginning. <FOO>
  113.       *    END_TERMINATION_MARKERS_KEY:    (Vector) list of marker that terminate the marker
  114.       *                                             when they are ending. </FOO>
  115.       *    IS_CONTAINER_KEY:              (String) "true" if the marker is a container. "false"
  116.       *                                            otherwise
  117.       *    SHOULD_RETAIN_FORMATTING_KEY (String) if "true", the parser does not remove \n ' ' and '\t'
  118.       *                                       from the data inside the container. This is useful
  119.       *                                       for markers like PRE
  120.       */
  121.     public void setRulesForMarker(Hashtable markerRules,String marker) {
  122.         rules.put( marker,markerRules );
  123.     }
  124.  
  125.  
  126.     /** Return the current rules for aMarker */
  127.     public Hashtable rulesForMarker(String aMarker) {
  128.         return (Hashtable) rules.get(aMarker);
  129.     }
  130.  
  131.     /** Convenience to set a single rule for a marker */
  132.     public void setRuleForMarker(String rule,Object value,String marker) {
  133.         Hashtable h = rulesForMarker(marker);
  134.         if( h == null )
  135.             h = new Hashtable();
  136.         h.put(rule,value);
  137.         setRulesForMarker(h,marker);
  138.     }
  139.  
  140.     /** Convenience to define the class that should be used to store an HTML
  141.       * component with the marker aMarker. If aMarker is STRING_MARKER_KEY or
  142.       * COMMENT_MARKER_KEY, this method will define which class should be used
  143.       * to store a String or comments.  aClass should be a subclass of
  144.       * TextViewHTMLElement.
  145.       */
  146.     public void setClassNameForMarker(String className,String aMarker) {
  147.         Hashtable r = rulesForMarker(aMarker);
  148.         if( r == null )
  149.             r = new Hashtable();
  150.         r.put(REPRESENTATION_KEY,className);
  151.         setRulesForMarker( r, aMarker );
  152.     }
  153.  
  154.     /** Return the name of the class that will be used to store a component with
  155.       * the marker aMarker if no specific class has been affected to the marker,
  156.       * this method will try the default classes. If no default exists, return
  157.       * null
  158.       */
  159.     public String classNameForMarker(String aMarker) {
  160.         Hashtable r = rulesForMarker(aMarker);
  161.         if( r != null ) {
  162.             String result = (String) r.get(REPRESENTATION_KEY);
  163.             if( result == null ) {
  164.                 if( isContainer(r) )
  165.                     result = defaultContainerClassName;
  166.                 else
  167.                     result = defaultMarkerClassName;
  168.             }
  169.             return result;
  170.         } else {
  171.             boolean isContainer;
  172.             Hashtable markerRules = rulesForMarker( aMarker );
  173.             if( markerRules != null ) {
  174.                 isContainer = isContainer( markerRules );
  175.             } else
  176.                 isContainer = true;
  177.  
  178.             if( isContainer && defaultContainerClassName != null )
  179.                 return defaultContainerClassName;
  180.             else if (!isContainer && defaultMarkerClassName != null )
  181.                 return defaultMarkerClassName;
  182.         }
  183.         return null;
  184.     }
  185.  
  186.     /** Set the name of the default class to be use to store container
  187.       * components This class is used if no other class has been specified by
  188.       * using setClassNameForMarker() or setRulesForMarker()
  189.       */
  190.     public void setDefaultContainerClassName(String aClassName) {
  191.         defaultContainerClassName = aClassName;
  192.     }
  193.  
  194.     /** Return the default container class */
  195.     public String defaultContainerClassName() {
  196.         return defaultContainerClassName;
  197.     }
  198.  
  199.     /** Set the name of the default class to be use to store markers components
  200.       * This class is used if no other class has been specified by using
  201.       * setClassForMarker() or setRulesForMarker()
  202.       */
  203.     public void setDefaultMarkerClassName(String aClassName) {
  204.         defaultMarkerClassName = aClassName;
  205.     }
  206.  
  207.     /** Return the default marker class */
  208.     public String defaultMarkerClassName() {
  209.         return defaultMarkerClassName;
  210.     }
  211.  
  212.     /** Set the name of the class that should be used to store a String
  213.       * The class should be a subclass of TextViewHTMLElement.
  214.       */
  215.     public void setStringClassName(String className) {
  216.         setClassNameForMarker(className, STRING_MARKER_KEY );
  217.     }
  218.  
  219.     /** Return the class that is used to store a String */
  220.     public String classNameForString() {
  221.         return classNameForMarker( STRING_MARKER_KEY );
  222.     }
  223.  
  224.     /** Set the name of the class that should be used to store a comment
  225.       * The class should be a subclass of TextViewHTMLElement.
  226.       */
  227.     public void setClassNameForComment(String className) {
  228.         setClassNameForMarker(className, COMMENT_MARKER_KEY );
  229.     }
  230.  
  231.     /** Return the class name that is used to store a comment */
  232.     public String classNameForComment() {
  233.         return classNameForMarker( COMMENT_MARKER_KEY );
  234.     }
  235.  
  236.     boolean shouldIgnoreEnd(Hashtable markerRules) {
  237.         if( markerRules == null )
  238.             return false;
  239.         if( markerRules.get(SHOULD_IGNORE_END_KEY) != null &&
  240.             ((((String)markerRules.get(SHOULD_IGNORE_END_KEY))).toUpperCase()).equals("TRUE"))
  241.             return true;
  242.         else
  243.             return false;
  244.     }
  245.  
  246.     boolean isContainer(Hashtable markerRules) {
  247.         if( markerRules == null )
  248.             return true;
  249.         if( markerRules.get(IS_CONTAINER_KEY) != null &&
  250.             ((((String)markerRules.get(IS_CONTAINER_KEY))).toUpperCase()).equals("FALSE"))
  251.             return false;
  252.         else
  253.             return true;
  254.     }
  255.  
  256.  
  257.     boolean shouldFilterStringsForChildren(Hashtable markerRules) {
  258.         if( markerRules == null )
  259.             return true;
  260.         if( markerRules.get(SHOULD_RETAIN_FORMATTING_KEY) != null  &&
  261.             (((String)markerRules.get(SHOULD_RETAIN_FORMATTING_KEY)).toUpperCase()).equals("TRUE"))
  262.             return false;
  263.         else
  264.             return true;
  265.     }
  266.  
  267.     /* archiving */
  268.  
  269.  
  270.     /** Describes the HTMLParsingRules class's information.
  271.       * @see Codable#describeClassInfo
  272.       */
  273.     public void describeClassInfo(ClassInfo info) {
  274.         info.addClass("netscape.application.HTMLParsingRules", 1);
  275.         info.addField(RULES_KEY, OBJECT_TYPE);
  276.         info.addField(DEFAULT_CONTAINER_CLASS_NAME_KEY, OBJECT_TYPE);
  277.         info.addField(DEFAULT_MARKER_CLASS_NAME_KEY, OBJECT_TYPE);
  278.     }
  279.  
  280.     /** Encodes the HTMLParsingRules instance.
  281.       * @see Codable#encode
  282.       */
  283.     public void encode(Encoder encoder) throws CodingException {
  284.         encoder.encodeObject(RULES_KEY,rules);
  285.         encoder.encodeObject(DEFAULT_CONTAINER_CLASS_NAME_KEY,defaultContainerClassName);
  286.         encoder.encodeObject(DEFAULT_MARKER_CLASS_NAME_KEY,defaultMarkerClassName);
  287.     }
  288.  
  289.     /** Decodes the HTMLParsingRules instance.
  290.       * @see Codable#decode
  291.       */
  292.     public void decode(Decoder decoder) throws CodingException {
  293.         rules = (Hashtable) decoder.decodeObject(RULES_KEY);
  294.         defaultContainerClassName = (String) decoder.decodeObject(DEFAULT_CONTAINER_CLASS_NAME_KEY);
  295.         defaultMarkerClassName    = (String) decoder.decodeObject(DEFAULT_MARKER_CLASS_NAME_KEY);
  296.     }
  297.  
  298.     /** Finishes the HTMLParsingRules instance decoding.
  299.       * @see Codable#finishDecoding
  300.       */
  301.     public void finishDecoding() throws CodingException {
  302.     }
  303. }
  304.  
  305.